Software Tools for Earth and Environmental Science

–7th WEEK–

Emir Toker

04/12/2020

R Language - Part 3

  • Syllabus, Last Week and Book

  • R Language - Part 3

    • Read
    • Write
    • Plot
  • TakeHome - MidTerm Project

  • Additional Course - II

  • Next Week - R Programming

Syllabus, Last Week and Book

Syllabus

Extended Syllabus PDF

LINK

Book

PDF - (Pg. 127-133 and 150-155)

Homework - I and II

*due date: 07/12/2020 23:59

Homework - I

Create a Notebook

Homework - II

Practice - Data Types and Structures

Additional Course - II

Today, 15:00 - 16:00

R Language

R Language - Part 1 & Part 2

  • Basic Math, Assigment, Comment
  • Data Types - Classes
    • Numeric
    • Integer
    • Logical
    • Character
    • Special Values
  • Data Structures - Objects
    • Vector
    • Matrice
    • Array
    • Data Frame
    • List

R Language - Part 3

  • Read

  • Write

  • Plot

R Language - Part 1 & 2

Getting Started

  • Assignment; <-
  • Comment; #
  • Help; ?func .or. help(func)
  • Install Packages; install.packages()
  • Call from Library; library()
  • Basic Math;
    • addition; +
    • subtraction; -
    • multiplication; *
    • division; /
    • exponentiation; ^
    • the square root; sqrt

Data Types - Classes

  • Numeric
# Any number with (or without) a decimal point.
a <- 3
  • Integer
# Sub-class of the numeric class. The suffix L tells R to store.
a <- 3L
  • Logical
# TRUE or FALSE - Logical Operators. < , > , == , >= , <= , != ... 
a <- 3<2
  • Character
# Data type consists of letters or words. String. with quotes: " … "
a <- "3"

is.XXX() and class()

Coercion

Data Structures (R-Objects)

Data Structures (R-Objects)

Data Structures - (Atomic) Vector

Vector : The simplest data structure in R

name <- "emir"
surname <- "toker"

print(c(name,surname))     # c means “combine”

Data Structures - Matrice

Vectors indexed using two indices instead of one.

[ row, col ]

a <- c(1:3)
# str(a) and dim(a) and length(a)
b <- matrix(1:3, nrow = 1, ncol = 3)
# str(b) and dim(b) and length(b)

Data Structures - Matrix

a <- c(1:3)
b <- matrix(1:3, nrow = 1, ncol = 3)
## [1] 1 2 3
##      [,1] [,2] [,3]
## [1,]    1    2    3
c <- matrix(1:9, nrow = 3, ncol = 3)
##      [,1] [,2] [,3]
## [1,]    1    4    7
## [2,]    2    5    8
## [3,]    3    6    9
d <- matrix(1:9, nrow = 3, ncol = 3, byrow = TRUE)
##      [,1] [,2] [,3]
## [1,]    1    2    3
## [2,]    4    5    6
## [3,]    7    8    9

Data Structures - Array

str(arr)
##  int [1:4, 1:3, 1:2] 1 2 3 4 5 6 7 8 9 10 ...
# dim(arr)
# length(arr)
x <- 1:24
##  [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
arr <- array(x, dim = c(4,3,2)) #raw,col,level
## , , 1
## 
##      [,1] [,2] [,3]
## [1,]    1    5    9
## [2,]    2    6   10
## [3,]    3    7   11
## [4,]    4    8   12
## 
## , , 2
## 
##      [,1] [,2] [,3]
## [1,]   13   17   21
## [2,]   14   18   22
## [3,]   15   19   23
## [4,]   16   20   24

Data Structures - Array

[ row, col, level ]

Data Structures - Data Frame

  • Each element is of the same length, like a matrix.
  • A column can have different types.
  • BUT, all the elements within a column are the same type.

Data Structures - List

  • Lists are like atomic vectors because they group data into a one-dimensional set.
  • Lists are like data frame because they can group different types of data.
  • BUT, the length of elements is NOT important.

Data Structures - List

matrix <- matrix(data=1:4,nrow=2,ncol=2)
vector <- c(T,F,T,T)
var <- "hello"
data_frame <- new_df2

list  <- list(matrix,vector,var,data_frame)
class(list)
str(list)
dim(list)
length(list)

R Language - Part 3

R Language - Part 3

  • Read

  • Write

  • Plot

Read

First, we need a DATA to read.

Go to Course web page

https://emirtoker.github.io/Software_Tools_R_Github/index.html

Read

Save .txt data to your R Project Directory

Cekmekoy_Omerli_15min.txt

https://emirtoker.github.io/Software_Tools_R_Github/index.html

Read

Learn where you are, working directory

getwd()

List all directories

list.dirs()

List all files

list.files()

Use default dataset in R

library(help="datasets")
# for example co2
# print() and plot() co2

Read

Two main ways

  • Interface of R
  • Console

Read

Interface of R -> Environment -> Import Dataset

Read

Interface of R -> Environment -> Import Dataset

From Text (base)

## Read

Interface of R -> Environment -> Import Dataset

From Text (base)

Read

Import Dataset -> From Text (readr) -> Browse

Read

Import Dataset -> From Text (readr) -> URL Paste -> Update

Read

Import Dataset -> From Text

Read

Console

  • read.table()
  • read.delim()
  • read.csv()

Try on console

- read.table("Cekmekoy_Omerli_15min.txt")
- read.delim("Cekmekoy_Omerli_15min.txt")
- read.csv("Cekmekoy_Omerli_15min.txt")

help()

OPTIONS : header =TRUE, sep = “;”

Read

Console

url <- "https://web.itu.edu.tr/~tokerem/18397_Cekmekoy_Omerli_15min.txt"

urldata_txt <- read.table(url,
                          header=TRUE, 
                          sep=";")

BONUS : Attributes

my_data <- read.csv("Cekmekoy_Omerli_15min.txt", 
                    header=T, 
                    sep=";")
View(my_data)
print(my_data)
class(my_data)
str(my_data)
attributes(my_data)
my_data$temp
attributes(my_data)
attributes(my_data)$names
attributes(my_data)$names[7]
attributes(my_data)$names[7] <- "temperature"
attributes(my_data)
attributes(my_data)$row.names
attributes(my_data)$row.names[1]
attributes(my_data)$row.names[1] <- "first"

Plot

plot()
plot(my_data)
plot(my_data[7])
plot(my_data[,7])
plot(my_data$temperature)
help(plot)
OPTIONS
plot(my_data$temperature, 
     type = "l", main = "My Plot", 
     xlab = "Time", ylab = "Temperature")

Plot

Plot

Plot

Uptade Preview

Plot

Now, plot PRESSURE parameter, in my_data

plot(my_data$pressure, 
     type = "l", 
     main = "My Plot", 
     xlab = "Time", 
     ylab = "Pressure")

Plot

Problem

How can I fix the missing data?

I will just assign NA for now. HOW?

Two ways:

  • Read the data again, but this time with NA
  • Assign all -9999.0 numbers as NA
my_data_NA <- read.csv("Cekmekoy_Omerli_15min.txt", 
                      header=T, 
                      sep=";",
                      na.strings=-9999.0)
View(my_data_NA)
print(my_data_NA)

Problem

How can I fix the missing data?

I will just assign NA for now. HOW?

Two ways:

  • Read the data again, but this time with NA
  • Assign all -9999.0 numbers as NA
my_data$pressure
my_data$pressure==-9999.0

which(my_data$pressure==-9999.0)
index <- which(my_data$pressure==-9999.0)
my_data$pressure[index]
my_data$pressure[index] <- NA

View(my_data)
print(my_data)

Plot

Write

write.table(x=urldata_txt,
            file="somenewfile.txt"
            sep=";",
            na="-9999",
            row.names=FALSE)
write.table(x=my_data,
            file="my_data.txt",
            sep=";",
            na=-9999.0,
            row.names=FALSE)
write.table(x=my_data_NA,
            file="my_data_NA.csv",
            sep=";",
            na="-9999",
            row.names=FALSE)

write.table() and OPTIONS

Plot

foo <- c(1.1,2,3.5,3.9,4.2)
bar <- c(2,2.2,-1.3,0,0.2)
plot(foo,bar)
  • type the supplied coordinates (for example, as stand-alone points or joined by lines or both dots and lines).
  • main, xlab, ylab Options to include plot title, the horizontal axis label, and the vertical axis label, respectively.
  • col Color (or colors) to use for plotting points and lines.
  • lty Stands for line type. (for example, solid, dotted, or dashed).
  • lwd This controls the thickness of plotted lines.
  • xlim, ylim limits for the horizontal range and vertical range (respectively)

Plot

plot(foo,bar)
plot(foo,bar,type="l")
plot(foo,bar,type="b",main="My lovely plot",xlab="x axis label", ylab="location y")
plot(foo,bar,type="b",main="My lovely plot",xlab="",ylab="",col="red")

x <- 1:20
y <- c(-1.49,3.37,2.59,-2.78,-3.94,-0.92,6.43,8.51,3.41,-8.23,
-12.01,-6.58,2.87,14.12,9.63,-4.58,-14.78,-11.67,1.17,15.62)
plot(x,y,type="n",main="")
abline(h=c(-5,5),col="red",lty=2,lwd=2)
segments(x0=c(5,15),y0=c(-5,-5),x1=c(5,15),y1=c(5,5),col="red",lty=3,
lwd=2)
points(x[y>=5],y[y>=5],pch=4,col="darkmagenta",cex=2)
points(x[y<=-5],y[y<=-5],pch=3,col="darkgreen",cex=2)
points(x[(x>=5&x<=15)&(y>-5&y<5)],y[(x>=5&x<=15)&(y>-5&y<5)],pch=19,
col="blue")
points(x[(x<5|x>15)&(y>-5&y<5)],y[(x<5|x>15)&(y>-5&y<5)])
lines(x,y,lty=4)
arrows(x0=8,y0=14,x1=11,y1=2.5)
text(x=8,y=15,labels="sweet spot")
legend("bottomleft",
legend=c("overall process","sweet","standard",
"too big","too small","sweet y range","sweet x range"),
pch=c(NA,19,1,4,3,NA,NA),lty=c(4,NA,NA,NA,NA,2,3),
col=c("black","blue","black","darkmagenta","darkgreen","red","red"),
lwd=c(1,NA,NA,NA,NA,2,2),pt.cex=c(NA,1,1,2,2,NA,NA))

Plot

my_data_NA <- read.csv("Cekmekoy_Omerli_15min.txt", 
                      header=T, 
                      sep=";",
                      na.strings=-9999.0)
plot(my_data$temp, 
     type = "l", 
     main = "My Plot", 
     xlab = "Time", 
     ylab = "Temperature",
     col = "red")

Plot ggplot2

library('ggplot2')

gg <- ggplot(my_data_NA, aes(x=seq(1,121))) +
      geom_line(aes(y=temp)) +
      labs(title="My Time Series", 
           subtitle="Temperature for Omerli Station", 
           caption="Source: Meteorology Station", 
           y="Temperature", 
           x="Time Step")
           
plot(gg)

Plot ggplot2

library('ggplot2')

gg <- ggplot(my_data_NA, aes(x=seq(1,121), y=temp)) +
      geom_point(aes(col=temp, size=temp)) +
      geom_smooth(method="loess", se=F) +
      labs(title="My Time Series", 
           subtitle="Temperature for Omerli Station",
           y="Temperature", 
           x="Time Step")
           
plot(gg)

Practice - R Language

Practice - R Language

  1. Read and assign your csv data (Header or seperator ?). “Cekmekoy_Omerli_15min.txt”
  2. Check the class and structure of your new data.
  3. Take the “Temperature” parameter and assign it as a new variable.
  4. Plot the “temperature” vector.
  5. Print minimum temperature and find which element is the minimum in temperature vector.
  6. change the minimum value with NA and Print.
  7. Plot the new “temperature” vector.
  8. Replace these new temperature values with old temperature values located in your data frame.
  9. Write your data frame as a new csv file.

Practice - R Language

  1. Read and assign your csv data (Header or seperator ?). “18397_Cekmekoy_Omerli_15dk.txt”
mydata <- read.csv(file = "Cekmekoy_Omerli_15min.txt", 
                   header = TRUE, 
                   sep = ";")
mydata
##     sta_no year month day hour minutes temp precipitation pressure
## 1    18397 2017     7  26   18       0 23.9          0.00   1003.0
## 2    18397 2017     7  26   18      15 23.9          0.00   1003.1
## 3    18397 2017     7  26   18      30 23.8          0.00   1003.2
## 4    18397 2017     7  26   18      45 23.8          0.00   1003.2
## 5    18397 2017     7  26   19       0 23.6          0.00   1003.2
## 6    18397 2017     7  26   19      15 23.2          0.00   1003.1
## 7    18397 2017     7  26   19      30 23.2          0.00   1003.1
## 8    18397 2017     7  26   19      45 23.1          0.00   1003.1
## 9    18397 2017     7  26   20       0 23.0          0.00   1003.1
## 10   18397 2017     7  26   20      15 22.8          0.00   1003.0
## 11   18397 2017     7  26   20      30 22.5          0.00   1003.0
## 12   18397 2017     7  26   20      45 22.4          0.00   1003.0
## 13   18397 2017     7  26   21       0 22.2          0.00   1003.0
## 14   18397 2017     7  26   21      15 22.3          0.00   1003.0
## 15   18397 2017     7  26   21      30 22.2          0.00   1003.1
## 16   18397 2017     7  26   21      45 21.7          0.00   1003.1
## 17   18397 2017     7  26   22       0 21.9          0.00   1003.2
## 18   18397 2017     7  26   22      15 21.7          0.00   1003.3
## 19   18397 2017     7  26   22      30 21.6          0.00   1003.3
## 20   18397 2017     7  26   22      45 22.2          0.00   1003.4
## 21   18397 2017     7  26   23       0 22.2          0.00   1003.4
## 22   18397 2017     7  26   23      15 22.1          0.00   1003.5
## 23   18397 2017     7  26   23      30 22.3          0.00   1003.4
## 24   18397 2017     7  26   23      45 22.5          0.00   1003.4
## 25   18397 2017     7  27    0       0 22.3          0.00   1003.4
## 26   18397 2017     7  27    0      15 22.2          0.00   1003.2
## 27   18397 2017     7  27    0      30 22.5          0.00   1003.2
## 28   18397 2017     7  27    0      45 22.6          0.00   1003.2
## 29   18397 2017     7  27    1       0 22.6          0.00   1003.3
## 30   18397 2017     7  27    1      15 22.6          0.00   1003.4
## 31   18397 2017     7  27    1      30 22.6          0.00   1003.2
## 32   18397 2017     7  27    1      45 22.7          0.00   1003.2
## 33   18397 2017     7  27    2       0 22.6          0.00   1003.3
## 34   18397 2017     7  27    2      15 22.5          0.00   1003.2
## 35   18397 2017     7  27    2      30 22.6          0.00   1003.2
## 36   18397 2017     7  27    2      45 22.5          0.00   1003.1
## 37   18397 2017     7  27    3       0 22.5          0.00   1003.1
## 38   18397 2017     7  27    3      15 22.4          0.00   1003.0
## 39   18397 2017     7  27    3      30 22.5          0.00   1003.1
## 40   18397 2017     7  27    3      45 22.4          0.00   1003.3
## 41   18397 2017     7  27    4       0 22.5          0.00   1003.4
## 42   18397 2017     7  27    4      15 22.6          0.00   1003.5
## 43   18397 2017     7  27    4      30 23.0          0.00   1003.5
## 44   18397 2017     7  27    4      45 23.2          0.00   1003.5
## 45   18397 2017     7  27    5       0 24.2          0.00   1003.6
## 46   18397 2017     7  27    5      15 25.1          0.00   1003.5
## 47   18397 2017     7  27    5      30 25.5          0.00   1003.4
## 48   18397 2017     7  27    5      45 26.1          0.00   1003.3
## 49   18397 2017     7  27    6       0 27.1          0.00   1003.3
## 50   18397 2017     7  27    6      15 26.9          0.00   1003.3
## 51   18397 2017     7  27    6      30 27.6          0.00   1003.3
## 52   18397 2017     7  27    6      45 28.0          0.00   1003.2
## 53   18397 2017     7  27    7       0 28.4          0.00   1003.1
## 54   18397 2017     7  27    7      15 28.5          0.00   1003.1
## 55   18397 2017     7  27    7      30 29.3          0.00   1003.0
## 56   18397 2017     7  27    7      45 30.2          0.00   1002.9
## 57   18397 2017     7  27    8       0 30.1          0.00   1002.8
## 58   18397 2017     7  27    8      15 30.1          0.00   1002.8
## 59   18397 2017     7  27    8      30 30.4          0.00   1002.8
## 60   18397 2017     7  27    8      45 30.4          0.00   1002.8
## 61   18397 2017     7  27    9       0 30.8          0.00   1002.9
## 62   18397 2017     7  27    9      15 30.9          0.00   1002.8
## 63   18397 2017     7  27    9      30 31.0          0.00   1002.6
## 64   18397 2017     7  27    9      45 31.5          0.00   1002.6
## 65   18397 2017     7  27   10       0 31.2          0.00   1002.6
## 66   18397 2017     7  27   10      15 30.9          0.00   1002.4
## 67   18397 2017     7  27   10      30 30.9          0.00   1002.4
## 68   18397 2017     7  27   10      45 30.4          0.00   1002.3
## 69   18397 2017     7  27   11       0 30.4          0.00   1002.1
## 70   18397 2017     7  27   11      15 30.0          0.00   1001.9
## 71   18397 2017     7  27   11      30 29.2          0.00   1001.9
## 72   18397 2017     7  27   11      45 29.5          0.00   1001.7
## 73   18397 2017     7  27   12       0 29.4          0.00   1001.6
## 74   18397 2017     7  27   12      15 29.3          0.00   1001.3
## 75   18397 2017     7  27   12      30 29.6          0.00   1001.2
## 76   18397 2017     7  27   12      45 28.8          0.00   1001.3
## 77   18397 2017     7  27   13       0 29.0          0.00   1001.1
## 78   18397 2017     7  27   13      15 29.0          0.00   1001.2
## 79   18397 2017     7  27   13      30 29.2          0.00   1001.3
## 80   18397 2017     7  27   13      45 28.4          0.00   1001.5
## 81   18397 2017     7  27   14       0 27.8          0.00   1001.6
## 82   18397 2017     7  27   14      15 27.4          0.00   1001.6
## 83   18397 2017     7  27   14      30 26.6          0.00   1001.5
## 84   18397 2017     7  27   14      45 26.2          0.00   1001.2
## 85   18397 2017     7  27   15       0 25.8          0.00   1001.1
## 86   18397 2017     7  27   15      15 25.6          0.00   1001.0
## 87   18397 2017     7  27   15      30 25.4          0.00   1000.9
## 88   18397 2017     7  27   15      45 24.2          0.00   1001.8
## 89   18397 2017     7  27   16       0 19.2          7.01   1003.7
## 90   18397 2017     7  27   16      15 19.5          8.80   1003.2
## 91   18397 2017     7  27   16      30 20.1          0.25   1003.1
## 92   18397 2017     7  27   16      45 20.8          0.00   1003.7
## 93   18397 2017     7  27   17       0 21.2          1.13  -9999.0
## 94   18397 2017     7  27   17      15 21.4          0.02   1005.6
## 95   18397 2017     7  27   17      30 21.4          1.25   1005.4
## 96   18397 2017     7  27   17      45 21.4          2.75   1005.1
## 97   18397 2017     7  27   18       0 21.2          0.00   1005.1
## 98   18397 2017     7  27   18      15 21.0          0.00  -9999.0
## 99   18397 2017     7  27   18      30 20.8          0.00   1006.3
## 100  18397 2017     7  27   18      45 20.9          0.00  -9999.0
## 101  18397 2017     7  27   19       0 20.8          0.19   1005.7
## 102  18397 2017     7  27   19      15 20.7          0.00   1006.2
## 103  18397 2017     7  27   19      30 20.8          0.20   1003.6
## 104  18397 2017     7  27   19      45 20.8          0.22   1003.7
## 105  18397 2017     7  27   20       0 20.9          0.00  -9999.0
## 106  18397 2017     7  27   20      15 20.6          0.00  -9999.0
## 107  18397 2017     7  27   20      30 20.6          0.00   1005.1
## 108  18397 2017     7  27   20      45 20.5          0.00   1005.6
## 109  18397 2017     7  27   21       0 20.7          0.00   1005.5
## 110  18397 2017     7  27   21      15 20.8          0.00   1005.7
## 111  18397 2017     7  27   21      30 20.4          0.00   1005.6
## 112  18397 2017     7  27   21      45 20.4          0.00   1005.8
## 113  18397 2017     7  27   22       0 20.6          0.00   1005.8
## 114  18397 2017     7  27   22      15 20.5          0.00   1005.9
## 115  18397 2017     7  27   22      30 20.4          0.00   1006.0
## 116  18397 2017     7  27   22      45 20.5          0.00   1005.9
## 117  18397 2017     7  27   23       0 20.5          0.00   1005.9
## 118  18397 2017     7  27   23      15 20.6          0.00   1005.9
## 119  18397 2017     7  27   23      30 20.5          0.00   1006.0
## 120  18397 2017     7  27   23      45 20.5          0.00   1006.0
## 121  18397 2017     7  28    0       0 20.4          0.00   1006.0
##     relative_humidity
## 1                  94
## 2                  95
## 3                  96
## 4                  96
## 5                  96
## 6                  97
## 7                  97
## 8                  98
## 9                  98
## 10                 98
## 11                 98
## 12                 99
## 13                 99
## 14                 99
## 15                 99
## 16                 99
## 17                 99
## 18                 99
## 19                 99
## 20                100
## 21                100
## 22                100
## 23                100
## 24                100
## 25                100
## 26                100
## 27                100
## 28                100
## 29                100
## 30                100
## 31                100
## 32                100
## 33                100
## 34                100
## 35                100
## 36                100
## 37                100
## 38                100
## 39                100
## 40                100
## 41                100
## 42                100
## 43                100
## 44                100
## 45                100
## 46                 97
## 47                 84
## 48                 82
## 49                 79
## 50                 78
## 51                 78
## 52                 76
## 53                 76
## 54                 75
## 55                 73
## 56                 65
## 57                 57
## 58                 60
## 59                 53
## 60                 52
## 61                 51
## 62                 51
## 63                 50
## 64                 53
## 65                 52
## 66                 57
## 67                 58
## 68                 59
## 69                 60
## 70                 61
## 71                 65
## 72                 66
## 73                 67
## 74                 66
## 75                 68
## 76                 70
## 77                 68
## 78                 69
## 79                 69
## 80                 71
## 81                 72
## 82                 72
## 83                 77
## 84                 79
## 85                 80
## 86                 82
## 87                 84
## 88                 79
## 89                 99
## 90                100
## 91                100
## 92                100
## 93                100
## 94                100
## 95                100
## 96                100
## 97                100
## 98                100
## 99                100
## 100               100
## 101               100
## 102               100
## 103               100
## 104               100
## 105               100
## 106               100
## 107               100
## 108               100
## 109               100
## 110               100
## 111               100
## 112               100
## 113               100
## 114               100
## 115               100
## 116               100
## 117               100
## 118               100
## 119               100
## 120               100
## 121               100

Practice - R Language

  1. Read and assign your csv data (Header or seperator ?). “Cekmekoy_Omerli_15min.txt”
  2. Check the class and structure of your new data.
  3. Take the “Temperature” parameter and assign it as a new variable.
  4. Plot the “temperature” vector.
  5. Print minimum temperature and find which element is the minimum in temperature vector.
  6. change the minimum value with NA and Print.
  7. Plot the new “temperature” vector.
  8. Replace these new temperature values with old temperature values located in your data frame.
  9. Write your data frame as a new csv file.

Practice - R Language

  1. Check the class and structure of your new data.
## [1] "data.frame"
## 'data.frame':    121 obs. of  10 variables:
##  $ sta_no           : int  18397 18397 18397 18397 18397 18397 18397 18397 18397 18397 ...
##  $ year             : int  2017 2017 2017 2017 2017 2017 2017 2017 2017 2017 ...
##  $ month            : int  7 7 7 7 7 7 7 7 7 7 ...
##  $ day              : int  26 26 26 26 26 26 26 26 26 26 ...
##  $ hour             : int  18 18 18 18 19 19 19 19 20 20 ...
##  $ minutes          : int  0 15 30 45 0 15 30 45 0 15 ...
##  $ temp             : num  23.9 23.9 23.8 23.8 23.6 23.2 23.2 23.1 23 22.8 ...
##  $ precipitation    : num  0 0 0 0 0 0 0 0 0 0 ...
##  $ pressure         : num  1003 1003 1003 1003 1003 ...
##  $ relative_humidity: int  94 95 96 96 96 97 97 98 98 98 ...
## $names
##  [1] "sta_no"            "year"              "month"            
##  [4] "day"               "hour"              "minutes"          
##  [7] "temp"              "precipitation"     "pressure"         
## [10] "relative_humidity"
## 
## $class
## [1] "data.frame"
## 
## $row.names
##   [1]   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18
##  [19]  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35  36
##  [37]  37  38  39  40  41  42  43  44  45  46  47  48  49  50  51  52  53  54
##  [55]  55  56  57  58  59  60  61  62  63  64  65  66  67  68  69  70  71  72
##  [73]  73  74  75  76  77  78  79  80  81  82  83  84  85  86  87  88  89  90
##  [91]  91  92  93  94  95  96  97  98  99 100 101 102 103 104 105 106 107 108
## [109] 109 110 111 112 113 114 115 116 117 118 119 120 121

Practice - R Language

  1. Read and assign your csv data (Header or seperator ?). “Cekmekoy_Omerli_15min.txt”
  2. Check the class and structure of your new data.
  3. Take the “Temperature” parameter and assign it as a new variable.
  4. Plot the “temperature” vector.
  5. Print minimum temperature and find which element is the minimum in temperature vector.
  6. change the minimum value with NA and Print.
  7. Plot the new “temperature” vector.
  8. Replace these new temperature values with old temperature values located in your data frame.
  9. Write your data frame as a new csv file.

Practice - R Language

  1. Take the “Temperature” parameter and assign it as a new variable.
temp_data <- mydata$temp
temp_data
##   [1] 23.9 23.9 23.8 23.8 23.6 23.2 23.2 23.1 23.0 22.8 22.5 22.4 22.2 22.3 22.2
##  [16] 21.7 21.9 21.7 21.6 22.2 22.2 22.1 22.3 22.5 22.3 22.2 22.5 22.6 22.6 22.6
##  [31] 22.6 22.7 22.6 22.5 22.6 22.5 22.5 22.4 22.5 22.4 22.5 22.6 23.0 23.2 24.2
##  [46] 25.1 25.5 26.1 27.1 26.9 27.6 28.0 28.4 28.5 29.3 30.2 30.1 30.1 30.4 30.4
##  [61] 30.8 30.9 31.0 31.5 31.2 30.9 30.9 30.4 30.4 30.0 29.2 29.5 29.4 29.3 29.6
##  [76] 28.8 29.0 29.0 29.2 28.4 27.8 27.4 26.6 26.2 25.8 25.6 25.4 24.2 19.2 19.5
##  [91] 20.1 20.8 21.2 21.4 21.4 21.4 21.2 21.0 20.8 20.9 20.8 20.7 20.8 20.8 20.9
## [106] 20.6 20.6 20.5 20.7 20.8 20.4 20.4 20.6 20.5 20.4 20.5 20.5 20.6 20.5 20.5
## [121] 20.4

Practice - R Language

  1. Read and assign your csv data (Header or seperator ?). “Cekmekoy_Omerli_15min.txt”
  2. Check the class and structure of your new data.
  3. Take the “Temperature” parameter and assign it as a new variable.
  4. Plot the “temperature” vector.
  5. Print minimum temperature and find which element is the minimum in temperature vector.
  6. change the minimum value with NA and Print.
  7. Plot the new “temperature” vector.
  8. Replace these new temperature values with old temperature values located in your data frame.
  9. Write your data frame as a new csv file.

Practice - R Language

  1. Plot the “temperature” vector.
plot(temp_data)

Practice - R Language

  1. Read and assign your csv data (Header or seperator ?). “Cekmekoy_Omerli_15min.txt”
  2. Check the class and structure of your new data.
  3. Take the “Temperature” parameter and assign it as a new variable.
  4. Plot the “temperature” vector.
  5. Print minimum temperature and find which element is the minimum in temperature vector.
  6. change the minimum value with NA and Print.
  7. Plot the new “temperature” vector.
  8. Replace these new temperature values with old temperature values located in your data frame.
  9. Write your data frame as a new csv file.

Practice - R Language

  1. Print minimum temperature and find which element is the minimum in temperature vector.
print(min(temp_data))
## [1] 19.2
which(temp_data==19.2) # which(temp_data==min(temp_data))
## [1] 89

Practice - R Language

  1. Read and assign your csv data (Header or seperator ?). “Cekmekoy_Omerli_15min.txt”
  2. Check the class and structure of your new data.
  3. Take the “Temperature” parameter and assign it as a new variable.
  4. Plot the “temperature” vector.
  5. Print minimum temperature and find which element is the minimum in temperature vector.
  6. change the minimum value with NA and Print.
  7. Plot the new “temperature” vector.
  8. Replace these new temperature values with old temperature values located in your data frame.
  9. Write your data frame as a new csv file.

Practice - R Language

  1. change the minimum value with NA and Print.
temp_data[89] <- NA

temp_data[which(temp_data==19.2)] <- NA

temp_data[which(temp_data==min(temp_data))] <- NA

print(temp_data)
##   [1] 23.9 23.9 23.8 23.8 23.6 23.2 23.2 23.1 23.0 22.8 22.5 22.4 22.2 22.3 22.2
##  [16] 21.7 21.9 21.7 21.6 22.2 22.2 22.1 22.3 22.5 22.3 22.2 22.5 22.6 22.6 22.6
##  [31] 22.6 22.7 22.6 22.5 22.6 22.5 22.5 22.4 22.5 22.4 22.5 22.6 23.0 23.2 24.2
##  [46] 25.1 25.5 26.1 27.1 26.9 27.6 28.0 28.4 28.5 29.3 30.2 30.1 30.1 30.4 30.4
##  [61] 30.8 30.9 31.0 31.5 31.2 30.9 30.9 30.4 30.4 30.0 29.2 29.5 29.4 29.3 29.6
##  [76] 28.8 29.0 29.0 29.2 28.4 27.8 27.4 26.6 26.2 25.8 25.6 25.4 24.2   NA 19.5
##  [91] 20.1 20.8 21.2 21.4 21.4 21.4 21.2 21.0 20.8 20.9 20.8 20.7 20.8 20.8 20.9
## [106] 20.6 20.6 20.5 20.7 20.8 20.4 20.4 20.6 20.5 20.4 20.5 20.5 20.6 20.5 20.5
## [121] 20.4

Practice - R Language

  1. Read and assign your csv data (Header or seperator ?). “Cekmekoy_Omerli_15min.txt”
  2. Check the class and structure of your new data.
  3. Take the “Temperature” parameter and assign it as a new variable.
  4. Plot the “temperature” vector.
  5. Print minimum temperature and find which element is the minimum in temperature vector.
  6. change the minimum value with NA and Print.
  7. Plot the new “temperature” vector.
  8. Replace these new temperature values with old temperature values located in your data frame.
  9. Write your data frame as a new csv file.

Practice - R Language

  1. Plot the new “temperature” vector.

Practice - R Language

  1. Read and assign your csv data (Header or seperator ?). “Cekmekoy_Omerli_15min.txt”
  2. Check the class and structure of your new data.
  3. Take the “Temperature” parameter and assign it as a new variable.
  4. Plot the “temperature” vector.
  5. Print minimum temperature and find which element is the minimum in temperature vector.
  6. change the minimum value with NA and Print.
  7. Plot the new “temperature” vector.
  8. Replace these new temperature values with old temperature values located in your data frame.
  9. Write your data frame as a new csv file.

Practice - R Language

  1. Replace these new temperature values with old temperature values located in your data frame.
mydata$temp <- temp_data

mydata
##     sta_no year month day hour minutes temp precipitation pressure
## 1    18397 2017     7  26   18       0 23.9          0.00   1003.0
## 2    18397 2017     7  26   18      15 23.9          0.00   1003.1
## 3    18397 2017     7  26   18      30 23.8          0.00   1003.2
## 4    18397 2017     7  26   18      45 23.8          0.00   1003.2
## 5    18397 2017     7  26   19       0 23.6          0.00   1003.2
## 6    18397 2017     7  26   19      15 23.2          0.00   1003.1
## 7    18397 2017     7  26   19      30 23.2          0.00   1003.1
## 8    18397 2017     7  26   19      45 23.1          0.00   1003.1
## 9    18397 2017     7  26   20       0 23.0          0.00   1003.1
## 10   18397 2017     7  26   20      15 22.8          0.00   1003.0
## 11   18397 2017     7  26   20      30 22.5          0.00   1003.0
## 12   18397 2017     7  26   20      45 22.4          0.00   1003.0
## 13   18397 2017     7  26   21       0 22.2          0.00   1003.0
## 14   18397 2017     7  26   21      15 22.3          0.00   1003.0
## 15   18397 2017     7  26   21      30 22.2          0.00   1003.1
## 16   18397 2017     7  26   21      45 21.7          0.00   1003.1
## 17   18397 2017     7  26   22       0 21.9          0.00   1003.2
## 18   18397 2017     7  26   22      15 21.7          0.00   1003.3
## 19   18397 2017     7  26   22      30 21.6          0.00   1003.3
## 20   18397 2017     7  26   22      45 22.2          0.00   1003.4
## 21   18397 2017     7  26   23       0 22.2          0.00   1003.4
## 22   18397 2017     7  26   23      15 22.1          0.00   1003.5
## 23   18397 2017     7  26   23      30 22.3          0.00   1003.4
## 24   18397 2017     7  26   23      45 22.5          0.00   1003.4
## 25   18397 2017     7  27    0       0 22.3          0.00   1003.4
## 26   18397 2017     7  27    0      15 22.2          0.00   1003.2
## 27   18397 2017     7  27    0      30 22.5          0.00   1003.2
## 28   18397 2017     7  27    0      45 22.6          0.00   1003.2
## 29   18397 2017     7  27    1       0 22.6          0.00   1003.3
## 30   18397 2017     7  27    1      15 22.6          0.00   1003.4
## 31   18397 2017     7  27    1      30 22.6          0.00   1003.2
## 32   18397 2017     7  27    1      45 22.7          0.00   1003.2
## 33   18397 2017     7  27    2       0 22.6          0.00   1003.3
## 34   18397 2017     7  27    2      15 22.5          0.00   1003.2
## 35   18397 2017     7  27    2      30 22.6          0.00   1003.2
## 36   18397 2017     7  27    2      45 22.5          0.00   1003.1
## 37   18397 2017     7  27    3       0 22.5          0.00   1003.1
## 38   18397 2017     7  27    3      15 22.4          0.00   1003.0
## 39   18397 2017     7  27    3      30 22.5          0.00   1003.1
## 40   18397 2017     7  27    3      45 22.4          0.00   1003.3
## 41   18397 2017     7  27    4       0 22.5          0.00   1003.4
## 42   18397 2017     7  27    4      15 22.6          0.00   1003.5
## 43   18397 2017     7  27    4      30 23.0          0.00   1003.5
## 44   18397 2017     7  27    4      45 23.2          0.00   1003.5
## 45   18397 2017     7  27    5       0 24.2          0.00   1003.6
## 46   18397 2017     7  27    5      15 25.1          0.00   1003.5
## 47   18397 2017     7  27    5      30 25.5          0.00   1003.4
## 48   18397 2017     7  27    5      45 26.1          0.00   1003.3
## 49   18397 2017     7  27    6       0 27.1          0.00   1003.3
## 50   18397 2017     7  27    6      15 26.9          0.00   1003.3
## 51   18397 2017     7  27    6      30 27.6          0.00   1003.3
## 52   18397 2017     7  27    6      45 28.0          0.00   1003.2
## 53   18397 2017     7  27    7       0 28.4          0.00   1003.1
## 54   18397 2017     7  27    7      15 28.5          0.00   1003.1
## 55   18397 2017     7  27    7      30 29.3          0.00   1003.0
## 56   18397 2017     7  27    7      45 30.2          0.00   1002.9
## 57   18397 2017     7  27    8       0 30.1          0.00   1002.8
## 58   18397 2017     7  27    8      15 30.1          0.00   1002.8
## 59   18397 2017     7  27    8      30 30.4          0.00   1002.8
## 60   18397 2017     7  27    8      45 30.4          0.00   1002.8
## 61   18397 2017     7  27    9       0 30.8          0.00   1002.9
## 62   18397 2017     7  27    9      15 30.9          0.00   1002.8
## 63   18397 2017     7  27    9      30 31.0          0.00   1002.6
## 64   18397 2017     7  27    9      45 31.5          0.00   1002.6
## 65   18397 2017     7  27   10       0 31.2          0.00   1002.6
## 66   18397 2017     7  27   10      15 30.9          0.00   1002.4
## 67   18397 2017     7  27   10      30 30.9          0.00   1002.4
## 68   18397 2017     7  27   10      45 30.4          0.00   1002.3
## 69   18397 2017     7  27   11       0 30.4          0.00   1002.1
## 70   18397 2017     7  27   11      15 30.0          0.00   1001.9
## 71   18397 2017     7  27   11      30 29.2          0.00   1001.9
## 72   18397 2017     7  27   11      45 29.5          0.00   1001.7
## 73   18397 2017     7  27   12       0 29.4          0.00   1001.6
## 74   18397 2017     7  27   12      15 29.3          0.00   1001.3
## 75   18397 2017     7  27   12      30 29.6          0.00   1001.2
## 76   18397 2017     7  27   12      45 28.8          0.00   1001.3
## 77   18397 2017     7  27   13       0 29.0          0.00   1001.1
## 78   18397 2017     7  27   13      15 29.0          0.00   1001.2
## 79   18397 2017     7  27   13      30 29.2          0.00   1001.3
## 80   18397 2017     7  27   13      45 28.4          0.00   1001.5
## 81   18397 2017     7  27   14       0 27.8          0.00   1001.6
## 82   18397 2017     7  27   14      15 27.4          0.00   1001.6
## 83   18397 2017     7  27   14      30 26.6          0.00   1001.5
## 84   18397 2017     7  27   14      45 26.2          0.00   1001.2
## 85   18397 2017     7  27   15       0 25.8          0.00   1001.1
## 86   18397 2017     7  27   15      15 25.6          0.00   1001.0
## 87   18397 2017     7  27   15      30 25.4          0.00   1000.9
## 88   18397 2017     7  27   15      45 24.2          0.00   1001.8
## 89   18397 2017     7  27   16       0   NA          7.01   1003.7
## 90   18397 2017     7  27   16      15 19.5          8.80   1003.2
## 91   18397 2017     7  27   16      30 20.1          0.25   1003.1
## 92   18397 2017     7  27   16      45 20.8          0.00   1003.7
## 93   18397 2017     7  27   17       0 21.2          1.13  -9999.0
## 94   18397 2017     7  27   17      15 21.4          0.02   1005.6
## 95   18397 2017     7  27   17      30 21.4          1.25   1005.4
## 96   18397 2017     7  27   17      45 21.4          2.75   1005.1
## 97   18397 2017     7  27   18       0 21.2          0.00   1005.1
## 98   18397 2017     7  27   18      15 21.0          0.00  -9999.0
## 99   18397 2017     7  27   18      30 20.8          0.00   1006.3
## 100  18397 2017     7  27   18      45 20.9          0.00  -9999.0
## 101  18397 2017     7  27   19       0 20.8          0.19   1005.7
## 102  18397 2017     7  27   19      15 20.7          0.00   1006.2
## 103  18397 2017     7  27   19      30 20.8          0.20   1003.6
## 104  18397 2017     7  27   19      45 20.8          0.22   1003.7
## 105  18397 2017     7  27   20       0 20.9          0.00  -9999.0
## 106  18397 2017     7  27   20      15 20.6          0.00  -9999.0
## 107  18397 2017     7  27   20      30 20.6          0.00   1005.1
## 108  18397 2017     7  27   20      45 20.5          0.00   1005.6
## 109  18397 2017     7  27   21       0 20.7          0.00   1005.5
## 110  18397 2017     7  27   21      15 20.8          0.00   1005.7
## 111  18397 2017     7  27   21      30 20.4          0.00   1005.6
## 112  18397 2017     7  27   21      45 20.4          0.00   1005.8
## 113  18397 2017     7  27   22       0 20.6          0.00   1005.8
## 114  18397 2017     7  27   22      15 20.5          0.00   1005.9
## 115  18397 2017     7  27   22      30 20.4          0.00   1006.0
## 116  18397 2017     7  27   22      45 20.5          0.00   1005.9
## 117  18397 2017     7  27   23       0 20.5          0.00   1005.9
## 118  18397 2017     7  27   23      15 20.6          0.00   1005.9
## 119  18397 2017     7  27   23      30 20.5          0.00   1006.0
## 120  18397 2017     7  27   23      45 20.5          0.00   1006.0
## 121  18397 2017     7  28    0       0 20.4          0.00   1006.0
##     relative_humidity
## 1                  94
## 2                  95
## 3                  96
## 4                  96
## 5                  96
## 6                  97
## 7                  97
## 8                  98
## 9                  98
## 10                 98
## 11                 98
## 12                 99
## 13                 99
## 14                 99
## 15                 99
## 16                 99
## 17                 99
## 18                 99
## 19                 99
## 20                100
## 21                100
## 22                100
## 23                100
## 24                100
## 25                100
## 26                100
## 27                100
## 28                100
## 29                100
## 30                100
## 31                100
## 32                100
## 33                100
## 34                100
## 35                100
## 36                100
## 37                100
## 38                100
## 39                100
## 40                100
## 41                100
## 42                100
## 43                100
## 44                100
## 45                100
## 46                 97
## 47                 84
## 48                 82
## 49                 79
## 50                 78
## 51                 78
## 52                 76
## 53                 76
## 54                 75
## 55                 73
## 56                 65
## 57                 57
## 58                 60
## 59                 53
## 60                 52
## 61                 51
## 62                 51
## 63                 50
## 64                 53
## 65                 52
## 66                 57
## 67                 58
## 68                 59
## 69                 60
## 70                 61
## 71                 65
## 72                 66
## 73                 67
## 74                 66
## 75                 68
## 76                 70
## 77                 68
## 78                 69
## 79                 69
## 80                 71
## 81                 72
## 82                 72
## 83                 77
## 84                 79
## 85                 80
## 86                 82
## 87                 84
## 88                 79
## 89                 99
## 90                100
## 91                100
## 92                100
## 93                100
## 94                100
## 95                100
## 96                100
## 97                100
## 98                100
## 99                100
## 100               100
## 101               100
## 102               100
## 103               100
## 104               100
## 105               100
## 106               100
## 107               100
## 108               100
## 109               100
## 110               100
## 111               100
## 112               100
## 113               100
## 114               100
## 115               100
## 116               100
## 117               100
## 118               100
## 119               100
## 120               100
## 121               100

Practice - R Language

  1. Read and assign your csv data (Header or seperator ?). “Cekmekoy_Omerli_15min.txt”
  2. Check the class and structure of your new data.
  3. Take the “Temperature” parameter and assign it as a new variable.
  4. Plot the “temperature” vector.
  5. Print minimum temperature and find which element is the minimum in temperature vector.
  6. change the minimum value with NA and Print.
  7. Plot the new “temperature” vector.
  8. Replace these new temperature values with old temperature values located in your data frame.
  9. Write your data frame as a new csv file.

Practice - R Language

  1. Write your data frame as a new csv file.
write.csv(mydata, file = "new_data.csv")

BONUS - Create a Function

What is Function ?

A function is a set of statements organized together to perform a specific task

ex: mean() (arithmetic mean)

x <- c(1,2,3)
mean(x)
## [1] 2
(1+2+3) / 3
## [1] 2

BONUS - Create a Function

What is Function ?

A function is a set of statements organized together to perform a specific task

ex: sample() (takes a sample of the specified size from the elements of x )

sample(c(1,6,32,7), size = 2)
## [1] 1 6

ex: sum() (returns the sum of all the values)

Practice

Create a Function

Problem: Take a sample belonged to population and sum

box <- 1:6                    # This is my population in a BOX
my_samp <- sample(box, size = 2) # This is my sample, I choose two var.
sum(my_samp)
box
samp

I want to create a new function named my_roll()

my_roll <- function(box) {
box <- 1:6 
my_samp <- sample(box) 
sum(my_samp)
}
my_roll()

Practice

Problem: I want to define population myself, in every time. remove pre-defined population box ?

my_roll2 <- function(box) {
my_samp <- sample(box, size = 2) 
sum(my_samp)
}
my_roll2(box)

box ?

box = 1:6
my_roll2(box)

my_roll2(box = 1:6)
my_roll2(1:6)

Practice

Create a Function

  • You can add new options
  • { } and () are important

Workshop - Midterm Project

Workshop - Midterm Project

  • Open a new R notebook
  • Go to course home page, (Midterm Project)
  • Click Rmd and Open “Midterm_Project.Rmd”
  • Copy all code and paste in your R notebook
  • Same way, open “Data” and paste file in your project directory.
  • Start to follow Instructions

Next Week

Next Week R Programming - PART I

Next Week R Programming - PART I